2012-10-03 171 views
1

使用從驗證插件的這個例子中「需要(依賴表達式)」的解釋:jQuery驗證規則依賴

$("#myform").validate({ 
    rules: { 
    details: { 
     required: "#other:checked" 
    } 
    }, debug:true 
}); 
$("#other").click(function() { 
    $("#details").valid(); 
}); 

我試圖做所需的文本輸入,如果單選按鈕#guide在選擇下面的例子:

<input type="radio" id="outfitter" name="memtype" value="Outfitter" />Outfitter $125 
<input type="radio" id="guide" name="memtype" value="Guide" />Guide $75 
<input type="text" id="sponout" name="sponout" size="75" /> 

我只是不知道在哪裏放置

$("#other").click(function() { 
    $("#details").valid(); 
}); 

規則驗證鱈魚內ING。

回答

2

這裏是你可以做到這一點的一種方法:

$("#myform").validate({ 
    rules: { 
     sponout: { 
      required: "#guide:checked" 
     } 
    } 
}); 

$("input[name='memtype']").change(function() { 
    $("#myform").valid(); 
}); 

例子:http://jsfiddle.net/kqczf/1/

+0

OK,疑難雜症。我會給呃呃。 Thanx,特別是小提琴的例子。 – robincham

+0

丟棄了最終的'.change函數'代碼部分,工作正常。實現我可以簡單地添加必要條件:「條件術語」,正如你所指出的,安德魯。 – robincham

+0

很高興能幫到你!如果幫助':)'請接受答案' –