不太熟悉 JQuery,但我基本上正在尝试做与此处要求相同的事情:
Inputs disabled until previous input filled in
.. 这个问题已成功回答,这是最合适的答案:http://jsfiddle.net/WYhNm/ (我也在使用的代码)
...但是,就我而言,有一个异常(exception):如果该字段已经有一个值,并且该值不为空,则不应禁用它。这样做的原因是因为我正在使用允许在提交之前预览的 PHP/HTML5 表单 - 因此字段的值通过表单传递,并放回到字段中,因为它的值全部在同一页面上。
示例:
//PHP
$tag1 = $_POST["Tag1"];
$tag2 = $_POST["Tag2"];
$tag3 = $_POST["Tag3"];
$output ='<form method="post" name="post_form"
action="process_post.php">
<input type="text" class="tag" name="Tag1" value="' . $tag1 . '">
<input type="text" class="tag" name="Tag2" value="' . $tag2 . '" disabled>
<input type="text" class="tag" name="Tag3" value="' . $tag3 . '" disabled>
<button type="submit" name="submit" value="publish_post">Publish Post</button>
<button type="submit" name="submit" value="preview_post">Preview Post</button>
</form>';
我尝试使用我的 PHP 知识在 jQuery 中运行比较 - 但效果不太好,因为我不知道我真正在做什么:
var next_step = $(this).next('.tag');
var input = $(this).val('.tag');
if ($(this).val()) {
next_step.attr('disabled', false);
}
elseif ( input == "" ) { //Is elseif even valid here?
next_step.attr('disabled', true);
}
tl;dr - 使用 this JSFiddle ,如果“禁用”字段的 value
= 任何非空值,如何启用它们?
最佳答案
我希望我正确理解你的问题。
$('.step').change(function() {
var next_step = $(this).next('.step');
// If the element *has* a value
if ($(this).val()) {
next_step.attr('disabled', false);
}
// If the element doesn't have a value
else {
if(!next_step.val()) next_step.attr('disabled', true); // <--
}
});
仅当元素的值计算为 false 时才禁用。
if(!next_step.val()) next_step.attr('disabled', true); // <--
希望这有帮助!
关于javascript - JQuery - 禁用表单,除非值不为 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24543603/