如何自动输入

时间:2015-03-08 19:14:15

标签: javascript php jquery

<form role="form" method="post" action="tes.php">
<button type="button" id="setValueButton">xSmall</button>
<input data-max="5000" name="name[1]" type="text">
<button type="button" id="setValueButton">xSmall</button>
<input data-max="4000" name="name[2]" type="text">
<button type="button" id="setValueButton">xSmall</button>
<input data-max="1000" name="name[3]" type="text">
</form>
  1. 如果单击按钮xSmall
  2. ,如何自动将值输入到输入中
  3. 如果用户输入的值大于data-max,则将值设置为data-max。

1 个答案:

答案 0 :(得分:1)

首先,每个元素必须具有唯一的ID:

<form role="form" method="post" action="tes.php">
    <button type="button" id="setValueButton1">xSmall</button>
    <input data-max="5000" name="name[1]" type="text">
    <button type="button" id="setValueButton2">xSmall</button>
    <input data-max="4000" name="name[2]" type="text">
    <button type="button" id="setValueButton3">xSmall</button>
    <input data-max="1000" name="name[3]" type="text">
</form>

然后你需要使用javascript(jQuery中的例子)

$('#setValueButton1').on('click', function () {
    $('input[name="name[1]"]').val('text string');
});

当您单击第一个按钮时,此示例将在第一个框中输入文本字符串。

检测对输入的更改,请使用以下内容:

$('#setValueButton1').on('change', function () {
    if ('#setValueButton1').val() > '5000' {
        // set it as above
    }
}

https://jsfiddle.net/hmhf9mxf/

相关问题