传递多个字段并显示在文本字段中

时间:2012-12-14 08:28:40

标签: javascript jquery

通过参考SOF's Question的回答,我想修复一下,在页面加载后(在用户的个人资料页面上)自动选择选项值传递到目标字段。

我还有兴趣知道文本字段上的更改方法在失去焦点之前被触发。

Sample on jsFiddle

$(function () {
    $("#options").change(function () {
        setTarget(); // Something has changed so lets rebuild the target
    });
    $("#options2").change(function () {
        setTarget(); // Something has changed so lets rebuild the target
    });
});

// Just get the values you want and update the target
function setTarget() {
    var tmp = $("#options").val();
    tmp += $("#options2").val();
    $('#targetTextField').val(tmp);
}

1 个答案:

答案 0 :(得分:0)

  1. 要在页面加载时传递所选的选项值,请在$函数中使用此事件:

    $(function () {
        ...
    
        // Add it here
        setTarget();
    });
    
  2. 对于传输到值的文本字段的更改,请使用keyup事件而不是change

    $(function () {
        ...
    
        $("#options3").keyup(function () {
            setTarget(); // Text has changed
        });
    
        ...
    });
    
  3. 看到这个小提琴:http://jsfiddle.net/scaillerie/e2ScF/49/