要做到這一點有一個簡單的方法,但我嘗試了所有我能想到的(可能不多說)。在窗體的下拉列表中選擇「添加新」選項時,彈出一個模式。第一次彈出佔位符時出現,這是我想要的。但在此之後,最後輸入的值將顯示而不是佔位符。我怎樣才能讓佔位符每次出現?如何使模態文本字段不被前面的條目填充
這裏是jQuery的:
<script type="text/javascript">
var Classofentry = '';
$('#add-new-text').val() === ''; // Set input text field to blank
console.log($('#add-new-text').val()); // <--------------- This is filled
$('#upload_form option[value="addnew"]').click(function(){
// Show modal window
$('#add-new').modal('show');
// Get the class
var Classofentry = $(this).attr("class");
//console.log(Classofentry);
//$('#add-new-submit').val() == ''; // Set input text field to blank
//console.log($('#add-new-submit').val()); // <------------- Empty after first change
//$('#add-new-text').val() === ''; // Set input text field to blank
console.log($('#add-new-text').val()); // <--------------- This is filled
$('#add-new-submit').on('click', function(){
// Get new option from text field
var value = $('#add-new-text').val();
//console.log(value);
$.ajax({
type: "POST",
url: "<?php echo site_url(); ?>main/change_options",
data: {new_option: value, new_option_class: Classofentry},
//dataType: "html",
dataType: "json",
error: errorHandler,
success: success
});
function success(data)
{
if (data[1])
{
// Add new entry
$('#'+Classofentry).append("<option value='" + data[0] + "'selected=\"selected\">" + data[0] + "</option>");
//alert(data[1]);
}
else
{
// Select the nonunique value by emptying it and appending
$('#'+Classofentry).empty("<option value=''selected=\"selected\">" + data[0] + "</option>").append("<option value='" + data[0] + "'selected=\"selected\">" + data[0] + "</option>");
//alert(data[0]);
}
}
function errorHandler()
{
//alert('Error with AJAX!');
alert(data[0]);
}
$('#add-new-submit').unbind('click'); // This fixes the problem for multiple entries
$('#add-new').modal('toggle');
});
//$('#add-new-submit').unbind('click');
//$('#upload_form option[value="addnew"]').unbind('click');
});
</script>
和模態:
<!-- add-new field -->
<div class="modal small hide fade" id="add-new" tabindex="-1" role="dialog" aria-labelledby="add-new-fieldLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">√ó</button>
<h3 id="add-new-fieldLabel">Add New Field</h3>
</div>
<div class="modal-body">
<p>Would you like to add a new item?</p>
<input type="text" id="add-new-text" name="add-new-text" placeholder="Type the new option">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" id="add-new-submit" name="add-new-submit"/>Add</button>
</div>
</div><!-- /add-new field -->
,你可以看到我試着簡單的設置附加新的文本標籤的空白,這沒有按」工作。有什麼我可以做的HTML?
這工作,但我用$('#add-new-text')。val(''); (而不是像上面那樣設置$('#add-new-text')。val()=='',謝謝! – MysticalTautologist