表格中的两件事情相互依赖

时间:2016-02-01 22:00:21

标签: javascript jquery html

在我的表单中,我有这个HTML:

<select name="clientData[first]" >
    <option value="0">without</option>  
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>
<input type="text" name="clientData[second]">

我要做的就是如果我选择without我可以在<input>中输入内容。如果我选择了其他内容,则应禁用<input>。如果我先在<input>中输入内容,请停用<select> 我不知道该怎么做。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:5)

您可以挂钩change的{​​{1}}事件,然后根据需要切换select。试试这个:

input
$('select').change(function() {
    if ($(this).val() == '0') {
        $('input').prop('disabled', false);
    } else {
        $('input').prop('disabled', true).val('');
    }
});

答案 1 :(得分:2)

这可以在jQuery中完成

params.require(:sync).permit({id1: [:error, :balance]}, {id2: [{:error, :balance},{:error, :balance},{:error, :balance}]})

您需要侦听选择标记的更改。

您可以使用所选的选择器选择具有所选状态的任何元素。然后,您可以使用prop。

分配属性

小提琴

https://jsfiddle.net/tu1au8aw/

答案 2 :(得分:0)

Serializable
$('select').change(function() {
      if ($(this).val() == '0') {
        $('input').prop('disabled', false);
      }
        else {
          $('input').prop('disabled', true);
        }
      });

    $('input').on('input', function() {
      if ($(this).val() == "") {
        $('select').prop('disabled', false);
      } else {
        $('select').prop('disabled', true);
      }
    });
相关问题