检查是否使用动态ID选中复选框

时间:2017-08-04 07:55:17

标签: jquery html

我读了很多关于这个问题的问题,但无论我做什么,它都不起作用。

我有几个带动态ID的复选框,如:

More:
<input type="checkbox" name="more" id="checkbox-more-285">
<br> Extra:
<input type="checkbox" name="extra" id="checkbox-extra-285">

<button class="add_to_cart">
  Add to cart
</button>

数字285可以是任何东西,我在其他地方使用。

然后我有了这个jQuery:

jQuery('.add_to_cart').click(function(e) {
  e.preventDefault();

  if (jQuery("[id^='checkbox-more-']:checked")) {
    console.log('1');
  }

  if (jQuery("[id^='checkbox-extra-']:checked")) {
    console.log('2');
  }
  return false;
});

正如您在jsFiddle https://jsfiddle.net/上看到的那样,每当我点击添加到购物车按钮时,两个日志都在控制台上。

2 个答案:

答案 0 :(得分:3)

仅仅测试jQuery集合是不够的,这将永远是真实的。将.length属性添加到测试中,如下所示:

if (jQuery("[id^='checkbox-more-']:checked").length) { // etc...

答案 1 :(得分:0)

你可以使用

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    FloatingActionMenu materialDesignFAM;
    FloatingActionButton floatingActionButton1, floatingActionButton2, floatingActionButton3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        materialDesignFAM = (FloatingActionMenu) findViewById(R.id.material_design_android_floating_action_menu);
        floatingActionButton1 = (FloatingActionButton) findViewById(R.id.material_design_floating_action_menu_item1);
        floatingActionButton2 = (FloatingActionButton) findViewById(R.id.material_design_floating_action_menu_item2);
        floatingActionButton3 = (FloatingActionButton) findViewById(R.id.material_design_floating_action_menu_item3);
        floatingActionButton1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // first item
                Intent intent = new Intent(MainActivity.this, Menu2.class);
                startActivity(intent);
            }
        });
        floatingActionButton2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            }
        });
        floatingActionButton3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            }
        });


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open,
                R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        //add this line to display menu1 when the activity is loaded
        displaySelectedScreen(R.id.nav_menu1);
    }

jQuery("[id^='checkbox-more-']").is(":checked")
{
    console.log('1');
  }