このコントロールは、だから、複数のボタン
によって呼び出すことができるようにする必要があり、これらのボタンは、それらの結果を持っていた部分をレンダリングするアクションを呼び出しますか?
これを行う方法は複数あります。最も簡単な方法は次のとおりです。
<button id="btn1" class="btns" data-target="txt1" type="button">A</button>
<button id="btn2" class="btns" data-target="txt2" type="button">B</button>
<input type="text" id="txt1" />
<input type="text" id="txt2" />
<div id="render">
</div>
<script>
var ajaxActive = false;
$(function() {
$(".btns").on('click', function() { // Bind the onclick of the button, so any button with this class may call the following function
var _button = $(this);
getItems(_button);
});
});
function getItems(_button) {
var bind = function (_button, results) {
$("#render").empty();
$("#render").append(results); // Append the partialview to the current view's div
$("#render .itemResult").on('click', function() { // Bind the onclick of the result
var resultValue = $(this).text(); // Or any other value that come from the result
var targetId = "#" + _button.data('target'); // Id of the input (Target) which comes from the clicked button
$(targetId).val(resultValue); // Change the input target value with the result one
});
};
if (ajaxActive) {
$.get('/Controller/Action') // Get the partialview
.done(function (results) {
bind(_button, results);
});
}
else {
var results = simulateCall(); // Get the results
bind(_button, results);
}
}
function simulateCall() { // Simulate a call to the server
return "<div class='items'> <div class='itemResult'>ABC</div> <div class='itemResult'>DEF</div> </div>";
}
</script>
PS:ここで働いてdemo
は、私はそれがデータベース
Downvotersに行くシミュレートするために、「コール」のいくつかの並べ替えを置くことに注意してくださいます。..どのように私はこれをより良い質問にすることができますか? –
downvoterではなく、少し書式を整えて、あなたが持っているもののいくつかの例を追加して試したものは、この状況で与えられる一般的なアドバイスです。 – Charleh