问题
I have made a script which enables adding of dynamic inputs on click of a button and removing unwanted inputs with a corresponding x button. My need is to add the x-editable plugin on every newly created dynamic input. I'm using x-editable, in such way, that on a selected option from the x-editable popup, an input from the right side gets a value which corresponds to option selected.
I have made that work with static elements but with dynamic ones I have big problems.
First of all, together with all html structure, classes of all dynamic elements are generated with its classname + number of dynamic field. precisely I'm doing this with configured Fieldcount: var FieldCount = 1;
and then in part where the generation of html code was made you add something like class="privacy-dynamic'+ FieldCount +'"
. And then the first dynamic element gets the class in html code called privacy-dynamic2
, second gets privacy-dynamic3
and so on.
Now, my first conclusion is that I need somehow to add a similar option to x-editable in which I would create a script with same + FieldCount +
so every x-editable pop up would correspond to its 'result input' from left rather than one popup to all dynamically made inputs.
I tried to generate a x-editable script in the same way I generate my html structure and it did not worked. Silly try from me, I know, generating script with script, but I was desperate.
Truly I don't know how it could be solved, I'm a bit of a jquery noob, and I'm lost in it. Can it even be solved somehow?
Here is current situation in which you have the first static field with x-editable working, and dynamic fields with same structure for x-editable but without the script for them: http://jsfiddle.net/dzorz/QxMs7/
html:
<div class="container">
<input type="text" class="main_activity" id="main_activity" name="main_activity" placeholder="Main activity">
<div class="parentToDelegate">
<a href="#" id="privacy" class="privacy" data-type="select" data-pk="1" data-value="1" data-original-title="Select visibility">public</a>
<input type="text" id="privacy_result" class="privacy_result" value="1"/>
</div>
<div class="row">
<div id="InputsWrapper">
</div>
</div>
<div class="row">
<span id="AddMoreBox" class="btn btn-info pull-right"><i class="icon-plus"></i>Add More</span>
</div>
script:
//x-editable
$('.privacy').editable({
showbuttons: false,
unsavedclass: null,
type: 'select',
inputclass: 'input-medium privacy-select',
source: [
{value: 1, text: 'public'},
{value: 2, text: 'approved contacts only'},
{value: 3, text: 'matching contacts'},
{value: 4, text: 'invisible'}
],
});
$(function(){
$('.parentToDelegate').on('change keyup blur', ".privacy-select", function(){
$('.privacy_result').val($('.privacy-select').val());
}).blur();
});
//dynamic fields
$(document).ready(function() {
var MaxInputs = 5; //maximum input boxes allowed
var InputsWrapper = $("#InputsWrapper"); //Input boxes wrapper ID
var AddButton = $("#AddMoreBox"); //Add button ID
var x = InputsWrapper.length; //initlal text box count
var FieldCount=1; //to keep track of text box added
$(AddButton).click(function (e) //on add input button click
{
// if(x <= MaxInputs) //max input box allowed
// {
FieldCount++; //text box added increment
//add input box
$(InputsWrapper).append('\
<div>\
<input type="text" class="other_activity"\
name="other_activity" id="other_activity"\
placeholder="Other activity" style="margin:0px 15px 10px 0px"/>\
<a href="#" class="removeclass"><i class="icon-remove icon-remove-add"></i></a>\
<div class="parentToDelegate-dynamic'+ FieldCount +' parent-dynamic">\
<a href="#" id="privacy-dynamic" class="privacy-dynamic'+ FieldCount +'" data-type="select" data-pk="1" data-value="1" data-original-title="Select visibility">public</a>\
<input type="text" id="privacy-result-dynamic'+ FieldCount +'" name="privacy-result-dynamic'+ FieldCount +'" class="privacy-result-dynamic'+ FieldCount +' privacy_dynamic" value="1"/>\
</div>\
</div>');
x++; //text box increment
// }
return false;
});
$("body").on("click",".removeclass", function(e){ //user click on remove text
if( x > 1 ) {
$(this).parent('div').remove(); //remove text box
x--; //decrement textbox
}
$('.income_count').trigger('change');
return false;
});
});
css:
.container{
padding-top:100px
}
.privacy_result, .privacy_dynamic{
width: 40px;
}
.main_activity, .other_activity{
width: 140px;
}
.parentToDelegate{
display:inline;
}
.icon-remove-add{
margin-left: -10px;
margin-top: -8px;
}
.parent-dynamic{
display: inline;
top: -5px;
left: 10px;
position: relative;
}
Any help or advice is welcome, you can edit my jsfiddle freely and post it back
回答1:
I figured out a solution for this that allows the .editable
to fire off on the first click.
$(document).on('mousemove', function() {
$('.username').editable();
... and another other fields you need to apply this too.
});
Just apply a mousemove
event since a user is always going to have to move their mouse to click on the field. This works perfect for me.
回答2:
You can make it work on dynamic fields calling your function like this :
$(document).on('click', '.xedit',function (e) {
e.preventDefault();
$(this).editable(your function here);
});
Be sure you add the class .xedit to the dynamic fields.
I updated your fiddle to show how this is working: http://jsfiddle.net/javimarcos/m7sj2/
You should remove the id in the dynamic fields (they all have the same id).
回答3:
In this way we can add more x-editable selectbox dynamically
> Blockquote
/***
$.fn.editable.defaults.mode = 'popup';
$(document).on('click', '.xeditable_option',function (e) {
$(this).editable({source : [{value : '',text : 'Select Option'},
<?php foreach($option_array as $option_list){?>
{value : '<?php echo $option_list['option_id'];?>', text : '<?php echo $option_list['option_name'];?>' },<?php }?> ]});
});
**/
> Blockquote
来源:https://stackoverflow.com/questions/17568840/jquery-x-editable-plugin-on-dynamic-fields