我的项目中有一个动态来自数据库的复选框。这里我只放了一个,但它可能很多取决于数据库表条目
"命名="类别"类="类别">
当用户点击任何一个复选框时,我想使用jQuery触发onclick事件。我尝试但是当我点击第一个复选框时它只是火。 这是我尝试的代码..
$("[type=checkbox]").click(function () {
var category_array = new Array();
var size_array = new Array();
var color_array = new Array();
$.each($("input[name='category']:checked"), function() {
category_array.push($(this).val());
});
});
任何人都可以知道为什么它不起作用 ??在此先感谢:)
答案 0 :(得分:1)
最好使用更改事件
$("input[type=checkbox]").change(function() {
if(this.checked) {
var category_array = new Array();
var size_array = new Array();
var color_array = new Array();
$.each($("input[name='category']:checked"), function() {
category_array.push($(this).val());
});
}
});
答案 1 :(得分:1)
所有复选框的选择器为:
$('input[type=checkbox]')
现在用它做任何你想做的事!
对于动态复选框......
$(document).on('change', 'input[type=checkbox]', function(e) {
//DO YOUR THANG
});
答案 2 :(得分:0)
这将解决您的问题首先将您的html更改为:
public class DoctorAdapter extends ArrayAdapter<Doctors>{
ArrayList<Doctors> doctorList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
public DoctorAdapter(Context context, int resource, ArrayList<Doctors> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
doctorList = objects;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(R.id.photo);
holder.tvName = (TextView) v.findViewById(R.id.doctor);
holder.tvMobile = (TextView) v.findViewById(R.id.mobile);
holder.callButton = (Button) v.findViewById(R.id.btnCall);
holder.callButton.setTag(holder);
holder.callButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
String message= viewHolder.tvMobile.getText().toString();
Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();
}
});
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.imageview.setImageResource(R.drawable.noimage);
new DownloadImageTask(holder.imageview).execute(doctorList.get(position).getImage());
holder.tvName.setText(doctorList.get(position).getName());
holder.tvMobile.setText(doctorList.get(position).getMobile());
return v;
}
static class ViewHolder {
public ImageView imageview;
public TextView tvName;
public TextView tvMobile;
public Button callButton;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
请注意,<div class="checkbox">
<label class="lbl-cat"><input type="checkbox" value="<?php echo $row["id"]; ?>" name="category[]" class="category"> <?php echo $row["name"]; ?></label>
</div>
不是name="category[]"
最后改变你的js为同样的
name="category"