2011-11-22 92 views
1

我有一個函數,它將select中的值寫入輸入。我使用的是活的(「變化」,功能來觸發此。優化jQuery選擇器以定位正確的輸入元素

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

這工作得很好,但我需要的目標最接近的輸入,所以它只能寫入到一個輸入,而不是所有的人。我有什麼下面試着不起作用。如果可以的話,請幫助表示感謝。

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

UPDATE

這裏是對的jsfiddle一個鏈接,請注意,該功能沒有在這裏工作,但你可以看到我的html。http://jsfiddle.net/clintongreen/9x6kv/

+0

'最接近()'指*始祖匹配*元件。 – alex

+0

「最接近」是什麼意思?請向我們展示您的HTML。 – SLaks

+1

請在您的回答中包含html標記 – Andre

回答

1

你的小提琴,但它的作品:Click here

您的問題與您的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行,如果statments。

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 
+0

魔術,非常感謝您的幫助。乾杯 –

+0

沒問題!!! :d – sicks

0

如果你肯定至少有一個,你可以嘗試

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

,但未通過集中的第一,但它不具有的「最近」任何想法

第一兄弟?嘗試

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

但是,無論如何,這將得到任何出現在您的反應。完全取決於什麼 '最近' 意味着

+0

謝謝盧克我試過這些,但他們沒有工作,我已經添加到小提琴的鏈接,功能不工作,但你可以看到的HTML。乾杯 –

0

假設輸入是在DOM未來,這應該工作:

if ($(this).val() == 'MyValue') {$(this).next().val(VarValue);} 
+0

嗨,謝謝,但我很不幸地使用表,所以輸入是在下一個TD所以next()沒有工作。我在這個問題中加了一個小提琴。乾杯 –

1

UPDATE:終極密碼(如果有人需要)

$(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'); 
}); 
}); 
+0

嗨,謝謝,但我很不幸地使用表,所以輸入是在下一個TD所以next()沒有工作。乾杯使用表格時 –

+0

您好,感謝阿爾珀,但選擇不工作:( 看到這裏,http://jsfiddle.net/clintongreen/gStHG/3/ –

+0

檢查此撥弄它現在是workinkg的http://的jsfiddle。淨/ 9x6kv/6/ – Alper