单选按钮多维数组

时间:2015-11-01 09:52:46

标签: jquery html arrays radio-button

我正在使用单选按钮。

<input type="radio" value="1" name="report[1][AP]">
<input type="radio" value="2" name="report[1][AP]">

<input type="radio" value="1" name="report[1][DCI]">
<input type="radio" value="2" name="report[1][DCI]">

<input type="radio" value="1" name="report[2][AP]">
<input type="radio" value="2" name="report[2][AP]">

<input type="radio" value="1" name="report[2][DCI]">
<input type="radio" value="2" name="report[2][DCI]">

如果检查报告[1] [AP],则还应检查报告[1] [DCI],或者如果选中报告[1] [DCI],则还应检查报告[1] [AP]。

类似地;

如果检查报告[2] [AP],则还应检查报告[2] [DCI],或者如果检查报告[2] [DCI],则还应检查报告[2] [AP]。

我该怎么办?

我正在使用它:

$('input[type="radio"]').on('click', function(){
    var found = $(this).attr('name');
    var founds = found.substring(0, found.indexOf(']'))+"]";
    $("input[type="+founds+"]").prop('required',true);
});

错误:语法错误,无法识别的表达式:input [type = report_time [1]]

2 个答案:

答案 0 :(得分:0)

尝试这样的事情......

$('input[type="radio"]').on('click', function(){
    var namePattern = /([a-z]*)(\[[0-9]\])(\[[A-Z]*\])/i;
    var found = $(this).attr('name').match(namePattern);

    var required = '[AP]';
    if(found[3] === '[AP]') {
        required = '[DCI]';
    }

    var reqButtonName = found[1] + found[2] + required;
    var reqButtonWithValue1 = $('[name="' + reqButtonName + '"]')[0];
 // $(reqButtonWithValue1).prop('checked', 'checked');
    $(reqButtonWithValue1).prop('required', 'required');
});

这将使用value=1

检查单选按钮[DCI] / [AP]

答案 1 :(得分:0)

这里我在Angular 7单选按钮多维数组中实现

其背后的想法是,通过单选按钮,我们具有name属性,该属性使得可以按行或按列选择,但同时按行和按列选择。每当按钮单击时,我将行放在名称中,并使用javascript处理列。

  

这是HTML代码

protocol FeedTableViewProviderType: class {
    var data: Feed? { get set }
    var feed: [FeedItem] { get }
    var insertRows: (([IndexPath]) -> Void)? { get set }
    var didRequestMoreData: ((Int) -> Void)? { get set }
}

class FeedTableViewProvider: NSObject, FeedTableViewProviderType {

    var insertRows: (([IndexPath]) -> Void)?
    var didRequestMoreData: ((Int) -> Void)?

    var data: Feed? {
        didSet {
            guard let data = data else { return }
            self.addMoreRows(data.feed)
        }
    }

    private(set) var feed = [FeedItem]() {
        didSet {
            isPaginating = false
        }
    }

    private var isPaginating = false

    private func addMoreRows(_ data: [FeedItem]) {
        var indexPaths = [IndexPath]()

        data.indices.forEach { indexPaths.append(IndexPath(row: feed.count + $0, section: 0)) }

        feed.append(contentsOf: data.sorted(by: { $0.props.createdDate > $1.props.createdDate }))

        insertRows?(indexPaths)
    }

    private func requestNextPage() {
        guard let currentPage = data?.currentPage, let totalPages = data?.totalPages, currentPage < totalPages else { return }
        didRequestMoreData?(currentPage + 1)
    }
}

extension FeedTableViewProvider: TableViewProvider {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return feed.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.reuseID, for: indexPath)
        cell.textLabel?.text = "Cell # \(indexPath.row)"
        return cell
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.item == feed.count - 1 && !isPaginating {
            isPaginating = true
            requestNextPage()
        }
    }
}
  

这是我使用Java脚本实现的.ts文件代码

<section class="editor">
 <strong>Editor</strong>
  <div>
   <label>Add option</label>
   <button (click)="addOption()">+</button>
  <div *ngFor="let option of options;let i = index">
  <span>{{option.title +(i+1)}}</span><button (click)="deleteOption(i)" 
    *ngIf="options.length>2">X</button>
  </div>
 </div>
</section>
<hr>
<section>
 <strong>Builder</strong>
  <div class="builder">
    <label>Question title goes here</label><br>
    <div *ngFor="let option of options;let row_index = index">
     <span>{{option.title +(row_index+1)}}</span>
     <span *ngFor="let rank of options;let column_index=index">
       <input type="radio" name="{{row_index}}" class="{{column_index}}" 
         (click)="check(column_index,$event)">
     </span>
   </div>   
  </div>
</section>

enter image description here