javascript - JQuery - 禁用表单,除非值不为 NULL?

标签 javascript php jquery html

不太熟悉 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/

相关文章:

javascript - 使用 ASP.Net MVC Web Api 将 JSON 数据导出到 Excel 工作表

javascript - 用于街景的 Googlemap API V3.22 中的 fullScreenControl fullScreenControlOptions

php - 如果脚本在锁定期间超时或终止,PHP 文件锁定会发生什么情况?

jquery - 使用 jQuery ajax 调用 Restful Web 服务

javascript - 在 jquery 事件处理程序中使用时原型(prototype)属性未定义

javascript - 使用 jQuery 匹配动态添加的事件(作为属性)

javascript - 如何在单击按钮时使用参数更新 DataTable?

javascript - jquery 验证在内容占位符中不起作用

javascript - 是否可以从服务器端自动发布客户端表单?

php - 在包含多个对象的数组上使用 array_diff()