1

I have a <select> drop down that I'm inserting into my HTML via jQuery. I don't have control over the HTML but I do have control over the JavaScript.

I'm trying to prop a value to an <input> element when you select an option. Basically, I have a <select> element with <option id="1"> and <option id="2"> onChange of this <select> element;
If you select option#1 it should prop the value dummy1 to input[name="Check_Config_PaymentGateway_CreditCards_Login1"] and the value dummy2 to input[name="Check_Config_PaymentGateway_CreditCards_Password"]
If you select option#2 it should prop the value dummy3 to input[name="Check_Config_PaymentGateway_CreditCards_Login1"] and the value dummy4 to input[name="Check_Config_PaymentGateway_CreditCards_Password"]

I've been working on this jsFiddle.

I'm pretty new at JavaScript in general, let alone jQuery. This is way to sophisticated for my skill... If you can help me, I'd really appreciate it.

4

3 回答 3

0

试试下面的东西,

编辑:缓存输入文本框。

演示

var dummyValues = [['dummy1', 'dummy2'], ['dummy3', 'dummy4']];

var $loginInput = $('input[name=Check_Config_PaymentGateway_CreditCards_Login1]');
var $pwdInput = $('input[name=Check_Config_PaymentGateway_CreditCards_Password]');

$('#paymentDD').change(function () {
  var selectedVal = parseInt($(this).val(), 10) - 1;
  $loginInput.val(dummyValues [selectedVal][0]);
  $pwdInput.val(dummyValues [selectedVal][1]);
});

dummyValues在将移动选项添加到下拉列表时更新了 var。

于 2012-04-10T21:06:34.760 回答
0

只要有可能,我就喜欢缓存元素,所以我就是这么做的。我还看到您在 jsFiddle 中使用了最新的 jQuery(1.7.2),那么为什么不使用推荐的.on()方法,然后在加载时调用立即.change()填充?

你可以尝试这样的事情:

var $span = $('span#print_pagename'),
    $login = $('input[name="Check_Config_PaymentGateway_CreditCards_Login1"]'),
    $password = $('input[name="Check_Config_PaymentGateway_CreditCards_Password"]'),
    $select = $('<select><option id="1">1</option><option id="2">2</option></select>');

$span.after($select);

$select.on('change', function() {

    var $this = $(this);

    if ($this.val() === '1') {

        $login.val('dummy1');
        $password.val('dummy2');

    } else if ($this.val() === '2') { 

        $login.val('dummy3');
        $password.val('dummy4');

    }

}).change();
于 2012-04-10T21:14:43.503 回答
0

给你:http: //jsfiddle.net/VW9N6/6/

这将绑定到元素的change事件<select>以检测其值何时更改,然后更新<input>元素的值。

于 2012-04-10T21:15:25.183 回答