1

選択から入力に値を書き込む関数があります。これをトリガーするために live("change", 関数を使用しています。

if ($(this).val( ) == 'MyValue') {$(".my_input").val(VarValue);}

これは正常に機能しますが、最も近い入力をターゲットにする必要があるため、すべてではなく、その 1 つの入力にのみ書き込みます。以下で試したことは機能しません。できれば助けてください、ありがとう。

if ($(this).val( ) == 'MyValue') {$(this).closest(".my_input").val(VarValue);}

アップデート

ここに Jsfiddle へのリンクがあります。機能はここでは機能していませんが、私の html を見ることができます。http://jsfiddle.net/clintongreen/9x6kv/

4

4 に答える 4

1

あなたのフィドル、しかしそれはうまくいく:ここをクリック

jsに問題がありました-変更:

$(".clone_dept").live("change", function() { //<-- not really sure why live isn't working
    if ($(this).val() == 'Dept #1') {$(".clone_attendees").val(dept_1);}
    if ($(this).val() == 'Dept #2') {$(".clone_attendees").val(dept_1);}
    if ($(this).val() == 'Dept #3') {$(".clone_attendees").val(dept_1);}
    if ($(this).val() == 'Dept #4') {$(".clone_attendees").val(dept_1);}
    if ($(this).val() == 'Dept #5') {$(".clone_attendees").val(dept_1);}
    }).change(); // trigger once if needed
}); // <-- This shouldnt be here

に:

$(".clone_dept").change(function() {
    if ($(this).val() == 'Dept #1') {$(".clone_attendees").val(dept_1);}
    if ($(this).val() == 'Dept #2') {$(".clone_attendees").val(dept_2);}
    if ($(this).val() == 'Dept #3') {$(".clone_attendees").val(dept_3);}
    if ($(this).val() == 'Dept #4') {$(".clone_attendees").val(dept_4);}
    if ($(this).val() == 'Dept #5') {$(".clone_attendees").val(dept_5);}
}).change(); // trigger once if needed

これは実際には最も近い入力を選択する方法を扱っていませんが。あなたのフィドルの場合、それはそのクラスを使用している唯一の要素です。この行構造をコピーして再利用する場合は、これを使用して適切な入力を選択します。

if($(this).val() == 'Dept #1') {$(this).parent().next("td").children(".clone_attendees").val(dept_1);}

編集:さらに良いことに、5行のifステートメントではなく、配列を使用して適切な値を割り当てます。

var dept = ["Person #1, Person #2, Person #3, Person #4, Person #5","Person #6, Person #7, Person #8, Person #9, Person #10","Finance Persons","IT Persons","Marketing Persons"];

オプションタグに値を追加する

<option value=0>Dept #1</option>

その後

$(".clone_dept").change(function() {
    if($(this).val() == 'Dept #1') {
        $(this).parent()
        .next("td")
        .children(".clone_attendees")
        .val(dept[$(this).val()])
    }
}).change(); // trigger once if needed
于 2011-11-22T01:58:31.067 に答える
1

更新:最終コード(誰かが必要な場合)

$(document).ready(function(){
var $theme = $("#clonned").clone();
// The Function in Question
var dept_1 = "Person #1, Person #2, Person #3, Person #4, Person #5";
var dept_2 = "Person #6, Person #7, Person #8, Person #9, Person #10";
var dept_3 = "Finance Persons";
var dept_4 = "IT Persons";
var dept_5 = "Marketing Persons";

$(".clone_dept").live("change", function() {
    if ($(this).val() == 'Dept #1') {
        $(this).closest("tr").find("td:eq(1) .clone_attendees").val(dept_1);
    }
    if ($(this).val() == 'Dept #2') {
        $(this).closest("tr").find("td:eq(1) .clone_attendees").val(dept_2);
    }
    if ($(this).val() == 'Dept #3') {
        $(this).closest("tr").find("td:eq(1) .clone_attendees").val(dept_3);
    }
    if ($(this).val() == 'Dept #4') {
        $(this).closest("tr").find("td:eq(1) .clone_attendees").val(dept_4);
    }
    if ($(this).val() == 'Dept #5') {
        $(this).closest("tr").find("td:eq(1) .clone_attendees").val(dept_5);
    }
}).change(); // trigger once if needed
// Clone Function
$(".tr_clone_add").live('click', function() {
    var $newone = $theme.clone();
    $newone.attr("id", "clonned"+Math.floor(Math.random()*11));
    $newone.appendTo('.table_clone');
});
});
于 2011-11-22T01:25:57.103 に答える
0

少なくとも1つあることが確実な場合は、試すことができます

if ($(this).val( ) == 'MyValue') {$(".my_input").eq(0).val(VarValue);}

それはセットから最初のものを取得しますが、「最も近い」という考えはありません

最初の兄弟?試す

if ($(this).val( ) == 'MyValue') {$(this).siblings(".my_input").eq(0).val(VarValue);}

しかし、それはとにかくあなたの前に現れるものを取得します。「最も近い」の意味に完全に依存します

于 2011-11-22T01:15:29.887 に答える
0

入力がDOMの次であると仮定すると、これは機能するはずです。

if ($(this).val( ) == 'MyValue') {$(this).next().val(VarValue);}
于 2011-11-22T01:15:56.020 に答える